package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsTopicMapper;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/21 17:30
 */
@SuppressWarnings("all")
@Service
public class DtsTopicServiceImpl implements DtsTopicService {

    @Autowired
    DtsTopicMapper dtsTopicMapper;

    /**
     * @param title 标题
     * @param subtitle 子标题
     */
    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String order, String sort, String title, String subtitle) {
        IPage<DtsTopic> iPage = new Page<>(page,limit);

        QueryWrapper<DtsTopic> wrapper = new QueryWrapper<>();
        //判断查询条件
        if(title!=null ){
            wrapper.like("title",title);
        }
        if(subtitle!=null){
            wrapper.like("subtitle",subtitle);
        }
        //判断排序规则
        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }

        wrapper.eq("deleted", false);

        dtsTopicMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());

        return map;
    }

    @Override
    public void insert(DtsTopic dtsTopic) {
        dtsTopic.setAddTime(new Date());
        dtsTopic.setDeleted(false);
        dtsTopic.setUpdateTime(new Date());
        dtsTopic.setSortOrder(100);
        dtsTopicMapper.insert(dtsTopic);
    }

    @Override
    public void update(DtsTopic dtsTopic) {
        dtsTopic.setUpdateTime(new Date());
        dtsTopicMapper.updateById(dtsTopic);
    }

    @Override
    public void delete(DtsTopic dtsTopic) {
        dtsTopic.setDeleted(true);
        dtsTopicMapper.updateById(dtsTopic);
    }
}
